import {
  getDSNListApi,
  addDSNListApi,
  updateDSNListApi,
  deleteDSNListApi,
  getUpdateDSNListApi
} from '@/api/DSNmanage'

const dataManage = {
  namespaced: true,
  state: {
    dataList: [],
    currentConfig: {
      current : 1,
      pageSize: 10,
      total: 50,
      pageSizeOptions: ['10', '30', '50', '100'],
      showQuickJumper: true,
      showSizeChanger: true,
      showTotal: (total) => {
        let pageConfig = dataManage.state.currentConfig
        return `共 ${total} 条记录 第${pageConfig.current} / ${Math.ceil(total/pageConfig.pageSize)} 页`
      }
    },
    order: null,
    orderBy: null
  },

  mutations: {
    SET_DATA: (state, list) => {
      state.dataList = list
    },
    SET_TOTAL: (state, total) => {
      state.currentConfig.total = total
    },
    SET_PAGES: (state, pages) => {
      state.currentConfig.pageSize = pages
    },
    SET_CURRENT: (state, current) => {
      state.currentConfig.current = current
    },
    SET_ORDER: (state, {order, orderBy}) => {
      state.order = order
      state.orderBy = orderBy
    }
  },

  actions: {
    // 获取数据源列表
    getDSNList ({ state, commit }) {
      return new Promise((reslove, reject) => {
        const pageSize = state.currentConfig.pageSize,
              page = state.currentConfig.current,
              order = state.order,
              orderBy = state.orderBy
        getDSNListApi({ page, pageSize, order, orderBy })
          .then(res => {
            if (res.code ==0) {
              const { data } = res
              commit('SET_DATA', data.data)
              commit('SET_TOTAL', data.total)
              commit('SET_PAGES', data.size)
              commit('SET_CURRENT', data.current)
              reslove(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    getAllDatasourceList ({ state, commit }) {
      return new Promise((reslove, reject) => {
        getDSNListApi()
          .then(res => {
            if (res.code ==0) {
              reslove(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    addDSNList ({ commit }, form) {
      return new Promise((reslove, reject) => {
        addDSNListApi({ ...form })
          .then(res => {
            if (res.code ==0) {
              reslove(res)
            }else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    updateDSNList ({ commit }, form){
      return new Promise((reslove, reject) => {
        updateDSNListApi({ ...form })
          .then(res => {
            if (res.code ==0) {
              reslove(res)
            }else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    getUpdateDSNList ({ commit }, id) {
      return new Promise((reslove, reject) => {
        getUpdateDSNListApi({ id })
          .then(res => {
            if (res.code ==0) {
              reslove(res)
            }else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    deleteDSNList ({ commit }, id) {
      return new Promise((reslove, reject) => {
        deleteDSNListApi({ id })
          .then(res => {
            if (res.code ==0) {
              reslove(res)
            }else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    }
  },

  getters: {

  }
}

export default dataManage
